<template>
  <div class="house">
    <div class="head">
      <span class="iconfont icon-fanhui" @click="$router.back()"></span>
      <header>
        <SearchHome :width="width">
          <template>
            <i class="iconfont icon-dingwei"></i>
          </template>
        </SearchHome>
      </header>
    </div>
    <p class="flex-box" :class="{color:show}" v-show="show">
      <button class="btn1">取消</button>
      <button class="btn2">确定</button>
    </p>
    <van-sticky>
      <van-button type="primary">
        <van-dropdown-menu active-color="#21b97a">
          <van-dropdown-item title="区域" ref="area">
            <van-picker
              show-toolbar
              :columns="columns"
              @confirm="(value, index) => onConfirm(value, index, 'area')"
              @cancel="onCancel('area')"
              toolbar-position="bottom"
              item-height="34"
            />
          </van-dropdown-item>
          <van-dropdown-item title="方式" ref="way">
            <van-picker
              show-toolbar
              :columns="wayList"
              @confirm="(value, index) => wayConfirm(value, index, 'way')"
              @cancel="onCancel('way')"
              toolbar-position="bottom"
              item-height="34"
            />
          </van-dropdown-item>
          <van-dropdown-item title="租金" ref="rent">
            <van-picker
              show-toolbar
              :columns="rentList"
              @confirm="(value, index) => rentConfirm(value, index, 'rent')"
              @cancel="onCancel('rent')"
              toolbar-position="bottom"
              item-height="34"
            />
          </van-dropdown-item>
          <van-dropdown-item title="筛选" @open="showPopup">
            <van-popup
              v-model="show"
              position="right"
              :style="{ height: '100%' }"
              get-container="body"
            >
              <div class="filterBox" @click="onClick">
                <p>户型</p>
                <div class="roomType">
                  <span
                    v-for="item in roomType"
                    :data-value="item.value"
                    :key="item.value"
                    >{{ item.label }}</span
                  >
                </div>
                <p>朝向</p>
                <div class="oriented">
                  <span
                    v-for="item in oriented"
                    :data-value="item.value"
                    :key="item.value"
                    >{{ item.label }}</span
                  >
                </div>
                <p>楼层</p>
                <div class="floor">
                  <span
                    v-for="item in floor"
                    :data-value="item.value"
                    :key="item.value"
                    >{{ item.label }}</span
                  >
                </div>
                <p>房屋亮点</p>
                <div class="characteristic">
                  <span
                    v-for="item in characteristic"
                    :data-value="item.value"
                    :key="item.value"
                    >{{ item.label }}</span
                  >
                </div>
              </div>
            </van-popup>
          </van-dropdown-item>
        </van-dropdown-menu>
      </van-button>
    </van-sticky>
    <div class="render">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <div
          v-for="(item, index) in cityList"
          :key="index"
          class="house"
          @click="$router.push(`/detail/${item.houseCode}`)"
        >
          <!-- $router.push(`/articledetail/?id=${item.houseCode}`) -->
          <div class="pic">
            <img
              :src="'http://www.xiaoshuaipeng.com:8080' + item.houseImg"
              alt=""
            />
          </div>
          <div class="txt">
            <p>{{ item.title }}</p>
            <p>{{ item.desc }}</p>
            <p>{{ item.tags[0] }}</p>
            <span>{{ item.price }} <i>元/月</i></span>
          </div>
        </div>
      </van-list>
    </div>
  </div>
</template>

<script>
// 请求地址:  'http://www.xiaoshuaipeng.com:8080/houses'
// 请求方式: get
import { houseQue, list } from '@/api/houses'
import SearchHome from '@/components/SearchHome.vue'
export default {
  name: 'HousePage',
  components: {
    SearchHome
  },
  data () {
    return {
      light: false,
      cityList: [],
      width: 'width:294px',
      isLoading: false,
      loading: false,
      finished: false,
      columns: [],
      wayList: [],
      rentList: [],
      show: false,
      subway: null,
      cityId: 'AREA|dbf46d32-7e76-1196',
      area: null,
      rentType: null,
      price: null,
      start: 1,
      characteristic: [],
      floor: [],
      oriented: [],
      roomType: [],
      more: []
    }
  },
  created () {
    // this.list()
    this.loadingHo()
    this.areaAsync()
    this.way()
    this.priceAsync()
  },
  beforeRouteEnter (to, from, next) {
    console.log('from', from)
    // console.log(from.params.cityNa)
    localStorage.setItem('cityName', from.params.cityNa)
    next()
  },
  methods: {
    onClick (e) {
      // console.log(value)
      // console.log(e.target.tagName)
      this.light = !this.light
      if (e.target.tagName === 'SPAN') {
        if (this.light) {
          e.target.classList.add('active')
          // console.log(e.target.dataset.value)
          this.more.push(e.target.dataset.value)
          this.more = [...new Set(this.more)]
          // console.log(this.more.join(','))
        } else {
          e.target.classList.remove('active')
          this.more = this.more.filter(
            (item) => item !== e.target.dataset.value
          )
          // console.log(this.more.join(','))
        }
      }
    },
    onCancel (thisRef) {
      // 关闭弹窗
      this.$refs[thisRef].toggle(false)
    },
    rentConfirm (value, index, thisRef) {
      console.log(value, index, thisRef)
      this.priceArea = this.priceArea.filter((item) => item.label !== '不限')
      this.priceArea.forEach((item) => {
        if (item.label === value) {
          this.price = item.value
        }
      })
      console.log(this.price)
      // console.log(this.priceArea)
      // 调用list方法
      this.list()
      // 关闭窗口
      this.$refs[thisRef].toggle(false)
    },
    wayConfirm (value, index, thisRef) {
      console.log(value, index, thisRef)
      // 关闭窗口
      if (value === '整租') {
        this.rentType = 'true'
      } else if (value === '合租') {
        this.rentType = 'false'
      }
      // 调用list方法
      this.list()
      // 关闭窗口
      this.$refs[thisRef].toggle(false)
    },
    async onConfirm (value, index, thisRef) {
      // console.log(value, index, thisRef)
      // this.$toast(`当前值：${value}, 当前索引：${index}`)
      const areaTwo = this.columns[index[0]].children[index[1]].value
      const areaThree =
        this.columns[index[0]].children[index[1]].children[index[2]].value

      // 给区域赋值
      if (areaThree !== undefined && areaThree !== 'null') {
        console.log(111, areaThree)
        this.area = areaThree
      } else if (areaTwo !== 'null' && areaTwo !== /号线/g) {
        this.area = areaTwo
        console.log(222, areaTwo)
      }
      // 调用list方法
      this.list()

      // 关闭窗口
      this.$refs[thisRef].toggle(false)
    },
    showPopup () {
      this.show = true
    },
    loadingHo () {
      if (this.isLoading === false) {
        this.$toast.loading({
          message: '加载中...',
          forbidClick: true
        })
      }
    },
    async onLoad () {
      const obj = {
        cityId: this.cityId,
        area: this.area,
        subway: this.subway,
        rentType: this.rentType,
        more: this.more.join(','),
        price: this.price,
        start: this.start
      }
      const data = await list(obj)
      // console.log(data)
      this.start++
      this.loading = false
      this.$toast(`共找到${data.body.count}套房源`)
      if (this.cityList.length > data.body.count) {
        this.finished = true
      }
      this.cityList.push(...data.body.list)
      // console.log(this.cityList)
    },
    // 租金
    async priceAsync () {
      const data = await houseQue()
      console.log(data.body)
      this.priceArea = data.body.price
      data.body.price.forEach((item) => this.rentList.push(item.label))
    },
    // 方式 和 筛选
    async way () {
      const data = await houseQue()
      // console.log(data.body)
      data.body.rentType.forEach((item) => this.wayList.push(item.label))
      this.characteristic = data.body.characteristic
      this.floor = data.body.floor
      this.oriented = data.body.oriented
      this.roomType = data.body.roomType
    },
    // 区域
    async areaAsync () {
      const data = await houseQue()
      // console.log(data.body.subway)
      const arr = []
      const arr1 = []
      // columns区域列表
      this.columns.unshift({
        text: data.body.area.label,
        children: data.body.area.children.map((item) => ({
          text: item.label,
          children: (arr.children = [{ text: '' }].concat(
            item.children
              ? item.children.map((item) => ({
                text: item.label,
                value: item.value
              }))
              : null
          )),
          value: item.value
        })),
        value: data.body.area.value
      })
      // columns地铁列表
      this.columns.push({
        text: data.body.subway.label,
        children: data.body.subway.children.map((item) => ({
          text: item.label,
          children: (arr1.children = [{ text: '' }].concat(
            item.children
              ? item.children.map((item) => ({
                text: item.label,
                value: item.value
              }))
              : null
          )),
          value: item.value
        })),
        value: data.body.subway.value
      })
      // console.log(this.columns)
    },
    async list () {
      const obj = {
        cityId: this.cityId,
        area: this.area,
        subway: this.subway,
        rentType: this.rentType,
        price: this.price,
        more: this.more.join(',')
      }
      console.log(obj)
      const data = await list(obj)
      // console.log(data)
      this.$toast(`共找到${data.body.count}套房源`)
      this.cityList = data.body.list
      // setTimeout(() => {
      //   this.isLoading = true
      // }, 3000)
    }
  }
}
</script>

<style lang="less" scoped>
.head {
  display: flex;
  width: 100%;
  align-items: center;
  height: 45px;
  margin-bottom: 2px;
  header {
    // position: absolute;
    // left: 20px;
    // z-index: 999;
    height: 30px;
    // padding: 0 7px;
    background-color: #f6f5f6;
    .icon-dingwei {
      margin-top: 4px;
      font-size: 24px;
      color: #21b97a;
      text-align: center;
      line-height: 25px;
    }
  }
  .icon-fanhui {
    margin-left: 4px;
    font-size: 25px;
    color: #9fafcc;
  }
}
.van-button--primary {
  background-color: #fff;
  border: none;
  width: 100%;
  height: 40px;
  padding: 0;
  .van-button__text {
    display: block;
    width: 100%;
    /deep/ .van-dropdown-menu__bar {
      height: 40px;
      z-index: 1;
      box-shadow: none;
      border-bottom: 1px solid #e4e6f0;
    }
  }
}
.van-popup--right {
  width: 295px;
  top: 50%;
  right: 0;
}
/deep/ .van-overlay {
  background-color: rgba(0, 0, 0, 0.5);
}
.render {
  min-height: 800px;
  background-color: #fff;
  .house {
    display: flex;
    justify-content: space-between;
    width: 100%;
    height: 120px;
    padding: 5px 0;
    border-bottom: 1px solid #e4e6f0;
    .pic {
      width: 106px;
      margin: 15px 6px 0 15px;
      img {
        width: 102px;
        height: 76px;
      }
    }
    .txt {
      flex: 1;
      margin-top: 15px;
      p {
        font-weight: 700;
        font-size: 15px;
        &:nth-child(2) {
          color: #afb2b3;
          font-size: 12px;
          margin-top: 2px;
          font-weight: 400;
        }
        &:nth-child(3) {
          width: 48px;
          height: 20px;
          color: #39becd;
          font-size: 12px;
          line-height: 20px;
          text-align: center;
          background-color: #e1f5f8;
        }
      }
      span {
        font-size: 16px;
        font-weight: 700;
        color: #fa5741;
        i {
          font-size: 12px;
          font-style: initial;
        }
      }
    }
  }
}
.filterBox {
  margin: 14px 0;
  padding: 0 15px;
  p {
    margin: 20px 0;
    font-size: 15px;
  }
  div {
    display: flex;
    flex-wrap: wrap;
    border-bottom: 1px solid #e5e5e5;
    padding-bottom: 14px;
    span {
      width: 70px;
      height: 32px;
      border: 1px solid #ddd;
      margin: 0 18px 15px 0px;
      border-radius: 3px;
      line-height: 32px;
      text-align: center;
      font-size: 12px;
      color: #888;
    }
    .active {
      border: 1px solid #21b97a;
      color: #21b97a;
      background-color: #defaef;
    }
  }
}
.flex-box {
  display: flex;
  width: 78%;
  transition: all 0.3s;
  transform: translateX(292px);
  height: 50px;
  position: fixed;
  right: 0;
  bottom: 0;
  background-color: #fff;
  z-index: 5000;
  margin: 0;
  .btn1 {
    flex: 1 1;
    border: none;
    color: #21b97a;
  }
  .btn2 {
    border: none;
    flex: 2 1;
    color: #fff;
    background-color: #21b97a;
  }
}
.color {
  transform: translateX(0px);
}
</style>
